package data;

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
import com.sun.speech.freetts.audio.AudioPlayer;
import com.sun.speech.freetts.audio.SingleFileAudioPlayer;
import java.io.File;
import javax.sound.sampled.AudioFileFormat.Type;

/**
 * Class that handle Text to Audio process
 * @author lailiaidi
 */
public class Audio {

    private final String obStr_VOICE_NAME = "mbrola_us1"; //Female Voice
    private final String obStr_DEFAULT_VOICE_NAME = "kevin"; //"kevin16"
    private VoiceManager obVoiceManager; // manages all the voices for FreeTTS.
    private Voice obVoice;
    private String obStrMessage, obStrAudioName;

    public Audio(String obStrAudioName, String obStrMessage) {
        this.obStrMessage = obStrMessage;
        this.obStrAudioName = obStrAudioName;
        this.obVoiceManager = VoiceManager.getInstance();
        //Try to use MBROLA voice: us1 lady voice
        if (System.getProperty("mbrola.base") == null) {
            System.setProperty("mbrola.base", "/mbrola/");
        }
        this.obVoice = obVoiceManager.getVoice(obStr_VOICE_NAME);

        //If Mbrola lib is not available, we use default FreeTTS voice: kevin voice
        if (obVoice == null) {
            System.err.println("Cannot find a voice name " + obStr_VOICE_NAME
                    + ". We will try to use " + obStr_DEFAULT_VOICE_NAME);
            this.obVoice = obVoiceManager.getVoice(obStr_DEFAULT_VOICE_NAME);
        }
        System.err.println("We're using voice: " + this.obVoice.getName());

        obVoice.setPitch((float) 4.00);
        obVoice.setPitchShift((float) .005);
        obVoice.setPitchRange((float) 0.01);
        // "business", "casual", "robotic", "breathy"
        obVoice.setStyle(obStr_VOICE_NAME);
    }

    /* Synthesize speech.*/
    public void createVoice() {
        /* Allocates the resources for the obVoice. */
        obVoice.allocate();

        /* create a audioplayer to dump the output file, this path need to be changed */
        AudioPlayer audioPlayer = new SingleFileAudioPlayer(obStrAudioName, Type.WAVE);

        /* attach the audioplayer */
        obVoice.setAudioPlayer(audioPlayer);

        /* set the text that will be spoken in the audio file */
        obVoice.speak(obStrMessage);

        /* Clean up and leave.*/
        obVoice.deallocate();

        /* Close the audioplayer, so the file could be saved */
        audioPlayer.close();
    }

    public void deleteVoice() {
        String obFileName = obStrAudioName;
        if (!obFileName.contains(".wav")) {
            obFileName += ".wav";
        }
        // A File object to represent the filename
        File obFile = new File(obFileName);

        // Make sure the file or directory exists and isn't write protected
        if (!obFile.exists()) {
            throw new IllegalArgumentException("Delete: no such file or directory: " + obFileName);
        }

        if (!obFile.canWrite()) {
            throw new IllegalArgumentException("Delete: write protected: " + obFileName);
        }

        // If it is a directory, make sure it is empty
        if (obFile.isDirectory()) {
            String[] files = obFile.list();
            if (files.length > 0) {
                throw new IllegalArgumentException("Delete: directory not empty: " + obFileName);
            }
        }

        // Attempt to delete it
        boolean success = obFile.delete();
        if (!success) {
            throw new IllegalArgumentException("Delete: deletion failed");
        }
    }
}//Class end

